package com.hunttown.common.sharding.client.config;

import com.hunttown.common.sharding.client.datasource.MultipleDataSourcesService;
import com.hunttown.common.sharding.client.rule.RouteRule;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

public class TableConfig implements InitializingBean {
    private final Log log = LogFactory.getLog(TableConfig.class);
    private String dbIndexPrefix = "";
    private String dbIndexSuffix = "";
    private int dbCount;
    private int dbStartWith = 0;
    private String tbNamePrefix = "";
    private String tbNameSuffix = "";
    private int tableCount;
    private int tableStartWith = 0;
    private String[] dbIndices;
    private String routeField;
    private String tabNameField;
    private boolean needPadToAlign = true;
    private MultipleDataSourcesService dataSourcesService;
    private RouteRule routeRule;
    private String[] tableIndices;

    public TableConfig() {
    }

    public void setRouteRule(RouteRule routeRule) {
        this.routeRule = routeRule;
    }

    public RouteRule getRouteRule() {
        return this.routeRule;
    }

    public String[] getDbIndices() {
        return this.dbIndices;
    }

    public String[] getTableIndices() {
        return this.tableIndices;
    }

    public void setDbIndexPrefix(String dbIndexPrefix) {
        if (StringUtils.isBlank(dbIndexPrefix)) {
            this.dbIndexPrefix = "";
        } else {
            this.dbIndexPrefix = dbIndexPrefix;
        }
    }

    public void setDbIndexSuffix(String dbIndexSuffix) {
        if (StringUtils.isBlank(dbIndexSuffix)) {
            this.dbIndexSuffix = "";
        } else {
            this.dbIndexSuffix = dbIndexSuffix;
        }
    }

    public void setDbCount(int dbCount) {
        this.dbCount = dbCount;
    }

    public void setTbNamePrefix(String tbNamePrefix) {
        if (StringUtils.isBlank(tbNamePrefix)) {
            this.tbNamePrefix = "";
        } else {
            this.tbNamePrefix = tbNamePrefix;
        }
    }

    public void setTbNameSuffix(String tbNameSuffix) {
        if (StringUtils.isBlank(tbNameSuffix)) {
            this.tbNameSuffix = "";
        }

        this.tbNameSuffix = tbNameSuffix;
    }

    public void setTableCount(int tableCount) {
        this.tableCount = tableCount;
    }

    public String getRouteField() {
        return this.routeField;
    }

    public void setRouteField(String routeField) {
        this.routeField = routeField;
    }

    public String getTabNameField() {
        return this.tabNameField;
    }

    public void setTabNameField(String tabNameField) {
        this.tabNameField = tabNameField;
    }

    public void setDbStartWith(int dbStartWith) {
        this.dbStartWith = dbStartWith;
    }

    public void setTableStartWith(int tableStartWith) {
        this.tableStartWith = tableStartWith;
    }

    public void setNeedPadToAlign(boolean isNeedPadToAlign) {
        this.needPadToAlign = isNeedPadToAlign;
    }

    public void afterPropertiesSet() throws Exception {
        if (this.dbCount <= 0) {
            throw new IllegalStateException("illegal dbCount,it must be >0!");
        } else if (this.tableCount <= 0) {
            throw new IllegalStateException("illegal tableCount,it must be>0!");
        } else if (this.routeRule == null) {
            throw new IllegalArgumentException("routeRule cann't be null!");
        } else {
            this.dbIndices = this.createDbIndices(this.dbIndexPrefix, this.dbIndexSuffix, this.dbCount);
            this.tableIndices = this.createIndices(this.tbNamePrefix, this.tbNameSuffix, this.tableCount);
        }
    }

    private String[] createIndices(String prefix, String suffix, int count) {
        String[] indices = new String[count];
        int indexNumberSize = String.valueOf(count).length();
        int dbNum = this.tableStartWith;

        for (int i = 0; i < count; ++i) {
            String index = String.valueOf(dbNum++);
            if (this.needPadToAlign) {
                for (int var9 = indexNumberSize - index.length(); var9-- > 0; index = "0" + index) {
                    ;
                }
            }

            indices[i] = prefix + index + suffix;
        }

        return indices;
    }

    private String[] createDbIndices(String prefix, String suffix, int count) {
        Map<String, String> dataSourceMap = this.dataSourcesService.getDataSources();
        if (dataSourceMap.size() != count) {
            this.log.error("dataSourcesService data source count:" + this.dataSourcesService.getDataSources().size() + ", and the dbCount:" + count);
            return null;
        } else {
            String[] indices = new String[count];
            int indexNumberSize = String.valueOf(count).length();
            int tableNum = this.dbStartWith;

            for (int i = 0; i < count; ++i) {
                String index = String.valueOf(tableNum++);
                if (this.needPadToAlign) {
                    for (int var10 = indexNumberSize - index.length(); var10-- > 0; index = "0" + index) {
                        ;
                    }
                }

                indices[i] = (String) dataSourceMap.get(prefix + index + suffix);
            }

            return indices;
        }
    }

    public void setDataSourcesService(MultipleDataSourcesService dataSourcesService) {
        this.dataSourcesService = dataSourcesService;
    }
}